package com.example.administrator.daggerdemo.module;




import com.example.administrator.daggerdemo.MyApplication;
import com.example.administrator.daggerdemo.module.api.TestApi;
import com.example.administrator.daggerdemo.utils.GsonUtils;
import com.example.administrator.daggerdemo.utils.NetWorkUtil;

import com.example.administrator.daggerdemo.utils.ThreadUtils;


import java.io.File;
import java.io.IOException;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
* 网络请求工具类
* @author zhengjian
* create by 2016/6/8 9:15
*/
public class NetWorkUtils {


    private static TestApi voteAPIService;



  public static TestApi getXQXAPIservice() {
        if (voteAPIService == null) {
            voteAPIService = createRetrofit().create(TestApi.class);
        }

        return voteAPIService;
    }

    public static void resetService(){
        voteAPIService =null;
    }
    static HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
    private static Retrofit createRetrofit() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://xqx.beidougx.com:8260/bacy/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(GsonUtils.getGsonInstance()))
                .callbackExecutor(ThreadUtils.getPool()) //使用自有线程池
                .client(client)
                .build();
        return retrofit;
    }

    /**
     * 配置OkHttp网络客户端
     *
     * @param client
     */

    private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            if (NetWorkUtil.isNetWorkAvailable(MyApplication.getInstance())) {
                int maxAge = 60; // 在线缓存在1分钟内可读取
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, max-age=" + maxAge)
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 28; // 离线时缓存保存4周
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
            }
        }
    };

    private static File httpCacheDirectory = new File(MyApplication.getInstance().getCacheDir(), "zhihuCache");
    private static int cacheSize = 10 * 1024 * 1024; // 10 MiB
    private static Cache cache = new Cache(httpCacheDirectory, cacheSize);
    private static OkHttpClient client = new OkHttpClient.Builder()
            .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addInterceptor(logging)
            .cache(cache)
            .build();
}
